package phone.example.com.a36kr.activity.findactivity;

import android.media.Image;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.adapter.RecentLvAdapter;
import phone.example.com.a36kr.bean.PopWindowBean;
import phone.example.com.a36kr.bean.RecentBean;
import phone.example.com.a36kr.bean.UrlBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.tools.BaseActivity;
import phone.example.com.a36kr.tools.MyApp;

/**
 * Created by dllo on 16/8/26.
 */
public class FindRecentEventActivity extends BaseActivity implements View.OnClickListener {


    private ListView recentLv;
    private RecentLvAdapter recentLvAdapter;
    private RecentBean recentBean;
    private String url = UrlBean.FIND_REGISTER_ALL_URL;
    private PopupWindow popWnd;

    @Override
    protected int setLayout() {
        return R.layout.activity_recent_event_find;
    }

    @Override
    protected void initView() {
        recentLv = bindView(R.id.recent_event_lv);
        LinearLayout popLl = bindView(R.id.Recent_Event_pop);
        popLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });

        ImageView backIv = bindView(R.id.recent_back);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        initNet();

    }

    private void initNet() {

        recentBean = new RecentBean();
        NetTool netTool = new NetTool();
        netTool.getNetData(url, RecentBean.class, new NetTool.NetListener<RecentBean>() {
            @Override
            public void onSuccess(RecentBean recentBean) {
                Log.d("发现近期活动", "recentBean:" + recentBean);
                Log.d("FindRecentEventActivity", UrlBean.FIND_REGISTER_ALL_URL);
                recentLvAdapter = new RecentLvAdapter(MyApp.getContext());
                recentLvAdapter.setRecentBeen(recentBean);
                recentLv.setAdapter(recentLvAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }

    private void showPopWindow(){
        View contentView = LayoutInflater.from(FindRecentEventActivity.this).inflate(R.layout.items_pop_recent_find, null);
        popWnd = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popWnd.setContentView(contentView);
        TextView allPop = (TextView) contentView.findViewById(R.id.items_pop_all_find);// 全部
        allPop.setOnClickListener(this);
        TextView demoPop = (TextView) contentView.findViewById(R.id.items_pop_demo_find);// Demo Day
        demoPop.setOnClickListener(this);
        TextView spacePop = (TextView) contentView.findViewById(R.id.items_pop_space_find);// 氪空间
        spacePop.setOnClickListener(this);
        TextView incestPop = (TextView) contentView.findViewById(R.id.items_pop_invest_find);// 股权投资
        incestPop.setOnClickListener(this);
        TextView companyPop = (TextView) contentView.findViewById(R.id.items_pop_company_find);// 企业服务
        companyPop.setOnClickListener(this);
        TextView gsouPop = (TextView) contentView.findViewById(R.id.items_pop_gsou_find);// 极速融资
        gsouPop.setOnClickListener(this);

        popWnd.setFocusable(true);

        View rootview = LayoutInflater.from(FindRecentEventActivity.this).inflate(R.layout.activity_recent_event_find, null);
        popWnd.showAtLocation(rootview, Gravity.BOTTOM, 0, 200);

        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popWnd.dismiss();
                return false;
            }
        });

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.items_pop_all_find:
                url = UrlBean.FIND_REGISTER_ALL_URL;
                break;
            case R.id.items_pop_demo_find:
                url = UrlBean.FIND_REGISTER_DEMO_URL;
                break;
            case R.id.items_pop_space_find:
                url = UrlBean.FIND_REGISTER_Kr_SPACE_URL;
                break;
            case R.id.items_pop_invest_find:
                url = UrlBean.FIND_REGISTER_INVEST_URL;
                break;
            case R.id.items_pop_company_find:
                url = UrlBean.FIND_REGISTER_COMPANY_URL;
                break;
            case R.id.items_pop_gsou_find:
                url = UrlBean.FIND_REGISTER_GSOU_URL;
                break;
        }
        recentLv.setAdapter(null);
        initNet();
        popWnd.dismiss();
        Log.d("近期活动跳转URL", url);
    }



}
